﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SampleKeyValueDemo.Relationships
{
    public interface IErrorHandler
    {
        void HandleError(Exception ex);
    }

    public class ErrorSquelch<T> : IKeyValueDataStore<T>
    {
        IErrorHandler handler;
        IKeyValueDataStore<T> source;
        public ErrorSquelch(IKeyValueDataStore<T> source, IErrorHandler handler)
        {
            this.source = source;
            this.handler = handler;
        }

        private void HandleError(Exception ex)
        {
            if (this.handler != null)
            {
                try { this.handler.HandleError(ex); }
                catch { }
            }
        }

        public void Set(string key, T val)
        {
            try
            {
                this.source.Set(key, val);
            }
            catch (Exception ex)
            {
                this.HandleError(ex);
            }

        }

        public T Get(string key)
        {
            try
            {
               return this.source.Get(key);
            }
            catch (Exception ex)
            {
                this.HandleError(ex);
            }
            return default(T);
        }

        public void Remove(string key)
        {
            try
            {
                this.source.Remove(key);
            }
            catch (Exception ex)
            {
                this.HandleError(ex);
            }
        }
    }
}